home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.2 / IRIX 6.2 CD2.iso / dist / print.idb / usr / sbin / rmprinter.z / rmprinter
Encoding:
Text File  |  1996-06-10  |  5.7 KB  |  223 lines

  1. #!/bin/sh
  2. #Tag 0x00000700
  3. #**************************************************************************
  4. #*
  5. #*           Copyright (c) 1993 Silicon Graphics, Inc.
  6. #*            All Rights Reserved
  7. #*
  8. #*       THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF SGI
  9. #*
  10. #* The copyright notice above does not evidence any actual of intended
  11. #* publication of such source code, and is an unpublished work by Silicon
  12. #* Graphics, Inc. This material contains CONFIDENTIAL INFORMATION that is
  13. #* the property of Silicon Graphics, Inc. Any use, duplication or
  14. #* disclosure not specifically authorized by Silicon Graphics is strictly
  15. #* prohibited.
  16. #*
  17. #* RESTRICTED RIGHTS LEGEND:
  18. #*
  19. #* Use, duplication or disclosure by the Government is subject to
  20. #* restrictions as set forth in subdivision (c)(1)(ii) of the Rights in
  21. #* Technical Data and Computer Software clause at DFARS 52.227-7013,
  22. #* and/or in similar or successor clauses in the FAR, DOD or NASA FAR
  23. #* Supplement. Unpublished - rights reserved under the Copyright Laws of
  24. #* the United States. Contractor is SILICON GRAPHICS, INC., 2011 N.
  25. #* Shoreline Blvd., Mountain View, CA 94039-7311
  26. #**************************************************************************
  27. #*
  28. #* File: rmprinter
  29. #*
  30. #* $Revision: 1.10 $
  31. #*
  32. #* Description: Shell script for removing a printer from the
  33. #*    System V spooling system.
  34. #*
  35. #*    Usage: rmprinter [-x <dest printer>] [-f] printer ...
  36. #*
  37. #*    Normally, if print jobs are pending in the print queue, rmprinter
  38. #*    will not remove the printer. If the -f flag is specified, the
  39. #*    printer will be removed regardless of pending print jobs. In
  40. #*    this case the pending print jobs will not be printed. If the
  41. #*    -x flag is specified, any pending print jobs will be moved to
  42. #*    <dest printer> and then the printer will be removed.
  43. #*
  44. #*    More than one printer can be specified for removal at one time.
  45. #*    If an error occurs while removing one of the printers the
  46. #*    script will continue attempting to remove all printers specified
  47. #*    but will exit with a status indicating an error.
  48. #*
  49. #**************************************************************************
  50.  
  51.  
  52. # Well known directories and programs
  53.  
  54. LPUTIL_DIR=/usr/lib
  55. LPUTIL=$LPUTIL_DIR/lputil
  56. OUT_FILT="/usr/bin/pg -s -p more... -e"
  57. SPOOL_DIR=/var/spool/lp
  58. LPSTAT=/usr/bin/lpstat
  59. TRANS_DIR=/var/spool/lp/transcript
  60. OLDLOG_DIR=/var/spool/lp/etc/log
  61.  
  62.  
  63. # Global variables
  64.  
  65. FORCE=0
  66. DEST_PNAME=""
  67.  
  68.  
  69. #
  70. # Prints the program usage string
  71. #
  72. PrintUsage()
  73. {
  74.     echo "\nUsage: rmprinter [-x <dest printer>] [-f] printer ..."
  75.     echo "where:"
  76.     echo "\t-x Specifies the printer to which"
  77.     echo "\t   pending print jobs are to be moved."
  78.     echo "\t-f Specifies that printer should be removed"
  79.     echo "\t   regardless of pending print jobs."
  80.     echo " "
  81. }
  82.  
  83.  
  84. #
  85. # Checks that we are the super-user and verifies
  86. # that the System V spooling system is present
  87. #
  88. CheckPermSpooler()
  89. {
  90.     idstr=`id`
  91.     cid=`expr "$idstr" : '^[     ]*uid=.*(\(.*\))[     ]*gid='`
  92.     if [ "$cid" != "root" ]; then
  93.     echo "You must be logged in as root to use this program."
  94.     exit 1;
  95.     fi
  96.     if [ ! -r $SPOOL_DIR ]; then
  97.     echo "The System V spooling system is not installed on this system."
  98.     exit 1
  99.     fi
  100. }
  101.  
  102.  
  103. #########################################################################
  104. #
  105. # Main program
  106. #
  107.  
  108. #
  109. # Ensure that we are root and that the main System V
  110. # spooling system directory is present
  111. #
  112. CheckPermSpooler
  113.  
  114. #
  115. # Process command line arguments
  116. #
  117. while getopts fx: flag
  118. do
  119.     case $flag in
  120.     f)  FORCE=1
  121.         ;;
  122.     x)  DEST_PNAME=$OPTARG
  123.         ;;
  124.     \?) PrintUsage
  125.         exit 1
  126.         ;;
  127.     esac
  128. done
  129.  
  130. #
  131. # Verify we have at least one printer to remove
  132. #
  133. shift `expr $OPTIND - 1`
  134. if [ $# -eq 0 ]; then
  135.     echo "$0: No printer(s) specified for removal"
  136.     PrintUsage
  137.     exit 1
  138. fi
  139.  
  140. #
  141. # Verify that the destination printer, if any, exists
  142. #
  143. if [ -n "$DEST_PNAME" -a ! -d /var/spool/lp/request/$DEST_PNAME ]; then
  144.     echo "$0: Destination printer \"$DEST_PNAME\" does not exist"
  145.     exit 1
  146. fi
  147.  
  148. #
  149. # Now go through the list of printers removing each one
  150. #
  151. changed=0
  152. errflag=0
  153. for PRINTER
  154. do
  155.     #
  156.     # First verify that the specified printer exits
  157.     #
  158.     if [ ! -d /var/spool/lp/request/$PRINTER ]; then
  159.     echo "$0: Printer \"$PRINTER\" does not exist"
  160.     errflag=1
  161.     continue
  162.     fi
  163.  
  164.     #
  165.     # Make sure that if a destination printer is specified,
  166.     # that it is not the same as the printer to be removed
  167.     #
  168.     if [ "$DEST_PNAME" = "$PRINTER" ]; then
  169.     echo "$0: The printer being removed cannot be the destination printer"
  170.     errflag=1
  171.     continue
  172.     fi
  173.  
  174.     #
  175.     # Now check for outstanding requests. If they are found and there
  176.     # is no destination or forced removal, do not remove the printer.
  177.     #
  178.     if [ "$FORCE" = 0 ]; then
  179.     reqlist=`lpstat -o$PRINTER 2> /dev/null`
  180.     if [ -n "$reqlist" -a -z "$DEST_PNAME" ]; then
  181.         echo "$0: Printer \"$PRINTER\" not removed - print jobs pending."
  182.         echo "\tUse -f to force removal of \"$PRINTER\" or"
  183.         echo "\t-x to specify a new destination for the requests."
  184.         echo " "
  185.         echo "Print queue for \"$PRINTER\":"
  186.         echo " "
  187.             $LPSTAT -o$PRINTER | $OUT_FILT
  188.         errflag=1
  189.         continue
  190.     fi
  191.     fi
  192.  
  193.     #
  194.     # Now remove old cruft. This is old transcript and log stuff
  195.     #
  196.     rm -rf ${TRANS_DIR}/${PRINTER}-log* ${TRANS_DIR}/${PRINTER}.opt
  197.     rm -rf ${OLDLOG_DIR}/${PRINTER}-log*
  198.  
  199.     #
  200.     # Now do the real work of removing the printer
  201.     #
  202.     $LPUTIL remove "$PRINTER" "$DEST_PNAME"
  203.     if [ $? -ne 0 ]; then
  204.     echo "$0: Unable to remove printer - $LPUTIL command failed"
  205.     errflag=1
  206.     continue
  207.     fi
  208.     echo "Printer \"$PRINTER\" removed"
  209.     changed=1
  210. done
  211.  
  212. #
  213. # Inform the user of what has happened
  214. #
  215. if [ $changed -ne 0 ]; then
  216.     echo " "
  217.     echo "Here is your printing environment:"
  218.     echo " "
  219.     $LPSTAT -t | $OUT_FILT
  220. fi
  221.  
  222. exit $errflag
  223.